package com.pro.addresslist.tools;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;

import com.android.volley.VolleyError;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
/**
 * @author libs
 * */
public abstract class EncryptInterface {
	public Context mContext;
	public static Listener<String> mjsonObjListener;
	public static ErrorListener mErrorListtener;
	
	public EncryptInterface(Context context, Listener<String> listener, ErrorListener errorListener) {
		this.mContext = context;
		this.mjsonObjListener = listener;
		this.mErrorListtener = errorListener;
	}
	 //String way
	public abstract void onMySuccess(JSONObject result);
	public abstract void onMyError(VolleyError error);
	
	//Object way
	public ErrorListener errorListener() {
		mErrorListtener = new ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError error) {
				onMyError(error);
			}
		};
		return mErrorListtener;
	}	
	public Listener<String> jsonObjectListener() {
		mjsonObjListener = new Listener<String>() {

			@SuppressLint("NewApi") @Override
			public void onResponse(String response) {
				if(response.isEmpty()||response==null){
					Log.i("libs", "获得的值为空");
					
				}
				Log.i("libs", response);
				//解密
				String json=BASE64Encrypt.deciphering(response);
				Log.i("libs", json);
				JSONObject jsonObject = null;
				try {
					jsonObject = new JSONObject(json);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					Log.i("libs", e.getMessage());
				}
				onMySuccess(jsonObject);
			}
		};
		return mjsonObjListener;
	}
}
